package stu.day25_day32.day25.day2;
import stu.day25_day32.day25.all.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class hw1 {

    public TreeNode searchBST(TreeNode root, int val) {
        if(root==null){
            return null;
        }

        if(root.val==val){
            return root;
        }else if(root.val<val){
            return searchBST(root.right,val);
        }else {
           return searchBST(root.left,val);
        }
    }


    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root==null){
            return new TreeNode(val);
        }

        if(val>root.val){
            if(root.right==null){
                root.right = new TreeNode(val);
            }else {
                insertIntoBST(root.right,val);
            }
        }else {
            if(root.left==null){
                root.left = new TreeNode(val);
            }else {
                insertIntoBST(root.left,val);
            }
        }


        return root;

    }
   /**
    public boolean isValidBST(TreeNode root) {
        List<Integer> list =new ArrayList<>();
        in(root,list);
        int[] t = list.stream().mapToInt(Integer::valueOf).toArray();
        for (int i=1;i<t.length;i++){
            if(t[i]<=t[i-1]){
                return false;
            }
        }
        return true;
    }*/


    public void in(TreeNode root,List<Integer> list){
        if(root==null){
            return;
        }
        in(root.left,list);
        list.add(root.val);
        in(root.right,list);
    }


    public boolean isValidBST(TreeNode root) {
      return isBST(root,Integer.MIN_VALUE,Integer.MAX_VALUE);
    }

    public boolean isBST(TreeNode root,int min,int max){

        if(root==null){
            return true;
        }
        if(root.val<min || root.val>max){
            return false;
        }
        if(root.left!=null && root.left.val>=root.val){
            return false;
        }
        if(root.right!=null && root.right.val<=root.val){
            return false;
        }

        if(!isBST(root.left,min,root.val-1) || !isBST(root.right,root.val+1,max)){
            return false;
        }

        return true;

    }

    public static void main(String[] args) {
        int[] arr = new int[]{2,1,3,10,6,7};

        List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());

        list.add(9);
        //list.sort((o1,o2)->o2-o1);
        //list.forEach(System.out::println);

        int[] x = list.stream().mapToInt(Integer::valueOf).toArray();
        x[0] = 1;
        Arrays.sort(x);


        List<Integer> l = List.of(2,3,4,5,1);
        l.add(12);
        l.forEach(System.out::println);


    }


}
